package caidan;

import caidan.res.Resource;
import jinyilw.swing.visuals.comp.ActImageComp;

import javax.swing.JFrame;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

public class SettingView extends SetFoodDataDialog
{
	private static final long serialVersionUID = -7195737034738553648L;
	private ActImageComp previousComp;
	private ActImageComp nextComp;
	private FoodPanel foodPanel;

	public SettingView(JFrame jf)
	{
		super(jf);
		//		setDefaultDimensionRatio(0.7, 0.75);
		//		Dimension d = getDefaultDimension();
		//		if (d.width > 920)
		//			setSize(920, 530);
		//		else
		//		{
		//			int h = (int) (d.width * 0.58);
		//			if (d.height > h)
		//				d.height = h;
		//			else
		//				d.width = (int) (d.height / 0.58);
		//			setSize(d.width, d.height);
		//		}
		initListeners();
	}

	private void initListeners()
	{
		addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				int keyCode = e.getKeyCode();
				if (keyCode == KeyEvent.VK_RIGHT)
					nextAct();
				else if (keyCode == KeyEvent.VK_LEFT)
					previousAct();
				// System.out.println(keyCode);
			}
		});
		previousComp.addClickOperator((Object o) ->
		{
			previousAct();
		});
		nextComp.addClickOperator((Object o) ->
		{
			nextAct();
		});
	}

	@Override
	protected void initButtonContainer(Container buttonContainer)
	{
		previousComp = new ActImageComp(Resource.arrowIcon2);
		nextComp = new ActImageComp(Resource.arrowIcon1);
		buttonContainer.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
		buttonContainer.add(previousComp);
		buttonContainer.add(cancel);
		buttonContainer.add(save);
		buttonContainer.add(nextComp);
	}

	@Override
	protected void save()
	{
		foodData.setFoodName(nameField.getText());
		dispose();
	}

	@Override
	protected void cancel()
	{
		foodData.setFoodData(oldFoodData);
		dispose();
	}

	@Override
	public void setVisible(boolean b)
	{
		super.setVisible(b);
		if (b)
			requestFocus();
	}

	public void setView(FoodPanel fPanel)
	{
		foodPanel = fPanel;
		foodData = fPanel.getFoodData();
		oldFoodData.setFoodData(foodData);
		Image image = foodData.getFoodImage();
		imageComp.setImage(image);
		nameField.setText(foodData.getFoodName());
		propertyPanel.setFoodProperty(foodData.getFoodProperty());
		materialPanel.setFoodMaterial(foodData.getFoodMaterial());
	}

	public void update()
	{
		if (foodPanel != null)
			setView(foodPanel);
	}

	public void nextAct()
	{
		foodData.setFoodName(nameField.getText());
		ArrayList<FoodPanel> foodPanels = foodPanel.getCurrentList();
		int index = (foodPanels.indexOf(foodPanel) + 1) % foodPanels.size();
		FoodPanel fp = foodPanels.get(index);
		if (fp != null)
			setView(fp);
	}

	public void previousAct()
	{
		foodData.setFoodName(nameField.getText());
		ArrayList<FoodPanel> foodPanels = foodPanel.getCurrentList();
		int index = foodPanels.indexOf(foodPanel) - 1;
		if (index == -1)
			index = foodPanels.size() - 1;
		FoodPanel fp = foodPanels.get(index);
		if (fp != null)
			setView(fp);
	}
}
